;(function ($) {

    var Carousel = function (poster) {
        var self = this;
        this.poster = poster;
        this.posterMainItem = this.poster.find('ul.poster-list');
        this.nextBtn = this.poster.find('div.poster-next-btn');
        this.prevBtn = this.poster.find('div.poster-prev-btn');
        this.posterItems = this.posterMainItem.find('li.poster-item');
        this.posterFirstItem = this.posterItems.first();
        this.posterLastItem = this.posterItems.last();
        this.totalSize = this.posterMainItem.length;
        this.rightSlice = this.posterItems.slice(0, Math.floor(this.totalSize / 2));
        this.setting = {
            "width": 1000,
            "height": 270,
            "posterWidth": 640,
            "posterHeight": 270,
            "scale": 0.9,
            "speed": 500,
            "verticalAlign": "middle"
        };
        $.extend(this.setting, this.getSetting());
        this.setSettingValue();
        this.setPosterPos();
        this.nextBtn.click(function () {
            self.carouseRotate('left');
        })
        this.prevBtn.click(function () {
            self.carouseRotate('right');
        })
    };
    Carousel.prototype = {
        //旋转
        carouseRotate: function (dir) {
            var _this_ = this;
            if (dir == 'left') {
                this.posterItems.each(function () {
                    var self = $(this);
                    // console.log(self.prev().get(0));
                    var prev = self.prev().get(0) ? $(self.prev().get(0)) : _this_.posterLastItem;
                    var width = prev.css('width');
                    var height = prev.css('height');
                    var zIndex = prev.css('zIndex');
                    var opacity = prev.css('opacity');
                    var left = prev.css('left');
                    var top = prev.css('top');
                    // self.css({'zIndex':zIndex});
                    self.animate({
                        width:width,
                        height:height,
                        opacity:opacity,
                        left:left,
                        top:top
                    })
                })
            } else {
                this.posterItems.each(function () {
                    var self = $(this);
                    // console.log(self.next().get(0));
                    var next = self.next().get(0) ? $(self.next().get(0)) : _this_.posterFirstItem;
                    var width = next.css('width');
                    var height = next.css('height');
                    var zIndex = next.css('zIndex');
                    var opacity = next.css('opacity');
                    var left = next.css('left');
                    var top = next.css('top');
                    // self.css({'zIndex':zIndex});
                    self.animate({
                        width:width,
                        height:height,
                        opacity:opacity,
                        left:left,
                        top:top
                    })
                })

            }
        },
        //设置图片的垂直对齐方式
        setVerticalAlign: function (height) {
            var type = this.setting.verticalAlign;
            switch (type) {
                case 'top':
                    return 0;
                    break;
                case 'middle':
                    return (this.setting.height - height) / 2;
                    break;
                case 'bottom':
                    return this.setting.height - height;
                    break;
                default:
                    return (this.setting.height - height) / 2;
                    break;
            }
        },
        //设剩余帧的宽度高度
        setPosterPos: function () {
            var self = this;
            var sliceItem = this.posterItems.slice(1),
                sliceSize = sliceItem.length / 2,
                rightSlice = sliceItem.slice(0, sliceSize),
                leftSlice = sliceItem.slice(sliceSize),
                level = Math.floor(this.posterItems.length / 2),
                scale = this.setting.scale,
                rw = self.setting.posterWidth,
                rh = self.setting.posterHeight,
                gap = (this.setting.width - this.setting.posterWidth) / 2 / level;
            var firstPosterLeft = (this.setting.width - this.setting.posterWidth) / 2;
            //设置右边帧的宽度高度
            rightSlice.each(function (i) {
                level--;
                rw = rw * scale;
                rh = rh * scale;
                var j = i;
                var left = firstPosterLeft + self.setting.posterWidth + (++i) * gap - rw;
                var top = self.setVerticalAlign(rh);
                $(this).css({
                    zIndex: level,
                    width: rw,
                    height: rh,
                    opacity: 1 / ++j,
                    left: left,
                    top: top
                })
            });
            //设置左边真帧的参数
            // var length = leftSlice.length;
            var j = leftSlice.length - 1;
            // var m = leftSlice.length-1;
            leftSlice.each(function (i) {
                var x = i;
                scale = Math.pow(self.setting.scale, ++x);
                // console.log(Math.pow(0.9,1));
                var lw = self.setting.posterWidth * scale;
                var lh = self.setting.posterHeight * scale;
                var left = gap * j--;
                var top = self.setVerticalAlign(lh);
                // length--;
                $(this).css({
                    zIndex: j,
                    width: lw,
                    height: lh,
                    opacity: 1 / i,
                    left: left,
                    top: top
                })
            });
            /*            var x = 0;
                        var y=0;
                        // console.log(leftSlice);
                        for (var m = leftSlice.length; m > 0; m--) {
                            var j = 0;
                            var lw = self.setting.posterWidth * Math.pow(scale, m);
                            var lh = self.setting.posterHeight * Math.pow(scale, m);
                            // console.log(lw);
                            // console.log(lh);
                            var left = j * gap;
                            console.log(j);
                            var top = (self.setting.height - lh) / 2;
                            // console.log(left);
                            // console.log(top);
                            // console.log($(leftSlice).eq(i));
                            $(leftSlice).eq(x).css({
                                zIndex: x,
                                width: lw,
                                height: lh,
                                opacity: 1 / ++y,
                                left: left,
                                top: top
                            })
                            x++;
                            y++;
                            j++;

                        }*/


            // console.log(sliceItem.length);


        },
        //设置配置参数去控制基本的宽度高度
        setSettingValue:

            function () {
                this.poster.css({
                    width: this.setting.width,
                    height: this.setting.height
                });
                this.posterMainItem.css({
                    width: this.setting.width,
                    height: this.setting.height
                });
                var btnWidth = (this.setting.width - this.setting.posterWidth) / 2;
                this.nextBtn.css({
                    width: btnWidth,
                    height: this.setting.height,
                    zIndex: Math.floor(this.posterItems.length / 2)
                });
                this.prevBtn.css({
                    width: btnWidth,
                    height: this.setting.height,
                    zIndex: Math.floor(this.posterItems.length / 2)
                });
                this.posterFirstItem.css({
                    left: btnWidth,
                    zIndex: Math.floor(this.posterItems.length / 2)
                });
            }

        ,
        //获取人工配置参数
        getSetting: function () {
            var dataSetting = this.poster.attr('data-setting');
            if (dataSetting && dataSetting != '') {
                return $.parseJSON(dataSetting);
            } else {
                return {};
            }
        }

    };
    Carousel.init = function (posters) {
        var __this__ = this;
        posters.each(function () {
            new __this__($(this))
        })
    };
    window['Carousel'] = Carousel;
})(jQuery);